package com.red.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.red.constant.JwtClaimsConstant;
import com.red.constant.MessageConstant;
import com.red.context.BaseContext;
import com.red.dto.AdminsLoginDTO;
import com.red.dto.AdminsPageQueryDTO;
import com.red.entity.Admins;
import com.red.exception.BaseException;
import com.red.mapper.AdminsMapper;
import com.red.properties.JwtProperties;
import com.red.result.PageResult;
import com.red.service.AdminsService;
import com.red.utils.JwtUtil;
import com.red.vo.AdminsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author 刘英豪
 */
@Service
@Slf4j
public class AdminsServiceImpl extends ServiceImpl<AdminsMapper, Admins> implements AdminsService {
    @Autowired
    private AdminsMapper adminsMapper;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 管理员登录
     * @param dto
     * @return
     */
    public AdminsVO login(AdminsLoginDTO dto) {
        //判断dto是否有值
        if (dto==null){
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
        //查询管理员信息
        QueryWrapper<Admins> qw = new QueryWrapper<>();
        qw.eq("username",dto.getUsername());
        Admins admins = adminsMapper.selectOne(qw);
        //查不到 返回错误信息
         if (admins==null){
             throw new BaseException(MessageConstant.ACCOUNT_NOT_FOUND);
         }
         if (admins.getStatus()==0){
             throw new BaseException("此账号已经被冻结");
         }
        // 查到了 密码错误 返回密码或用户名错误
        String passWord = dto.getPassword();
        passWord = DigestUtils.md5DigestAsHex(passWord.getBytes());
        if (!passWord.equals(admins.getPassword())){
            throw new BaseException(MessageConstant.PASSWORD_ERROR);
        }
        //查到了 生成token
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,admins.getId());
        String jwt = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        //封装VO
        AdminsVO adminsVO =new AdminsVO();
        BeanUtils.copyProperties(admins,adminsVO);

        adminsVO.setToken(jwt);

        return adminsVO;
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    public PageResult pageQuery(AdminsPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Admins> page = adminsMapper.pageQuery(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询信息
     * @param
     * @return
     */
    public Admins getAdminInfo() {
        Long empId = BaseContext.getCurrentId();
        return adminsMapper.selectById(empId);
    }

    /**
     * 修改信息
     * @param admins
     */
    public void updateAdmins(Admins admins) {
        //查看当先线程的管理员Id
        Long empId = BaseContext.getCurrentId();
        Admins admins1 = adminsMapper.selectById(empId);
        if (Objects.equals(admins1.getRoles(), "admin")){
            adminsMapper.updateById(admins);
        }else {
            throw new BaseException("你没有修改权限");
        }
    }

    /**
     * 删除管理员
     * @param id
     */
    public void deleteById(Long id) {
        //查看当先线程的管理员Id
        Long empId = BaseContext.getCurrentId();
        Admins admins = adminsMapper.selectById(empId);
        if (!Objects.equals(admins.getRoles(), "admin")){
            throw new BaseException("你没有删除的权限");
        }
        adminsMapper.deleteById(id);
    }

    /**
     * 新增管理员
     * @param admins
     */
    public void insert(Admins admins) {
        //查看当先线程的管理员Id
        Long empId = BaseContext.getCurrentId();
        Admins admins1 = adminsMapper.selectById(empId);
        if (!Objects.equals(admins1.getRoles(), "admin")){
            throw new BaseException("你没有新增的权限");
        }else {
            admins.setPassword(DigestUtils.md5DigestAsHex(admins.getPassword().getBytes()));
            admins.setAvatar("https://s21.ax1x.com/2025/03/16/pEd9zWR.jpg");
            admins.setCreatedAt(LocalDateTime.now());
            adminsMapper.insert(admins);
        }
    }
}
